package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.Page;
import com.itheima.pojo.SetmealDto;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wuyiqiang
 * @2022/6/14 16:04
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "/setmeal",produces = "application/json;charset=utf-8")
public class SetmealController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐菜品
     * @param setmealDto
     * @param request
     * @return
     */
    @PostMapping
   public R save(@RequestBody SetmealDto setmealDto, HttpServletRequest request){
        String username = tokenToUsername(request);
        setmealDto.setUpdateUser(username);
        setmealDto.setCreateUser(username);

        setmealService.save(setmealDto);
        return R.success("新增套餐成功");
   }


    /**
     * 分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public  R<Page> page(@Param("name")String name,@Param("page")Integer page,@Param("pageSize")Integer pageSize){
        R<Page> result = setmealService.findByPage(page,pageSize,name);
        return result;
    }



    /**
     * 批量或者单独删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam("ids") List<String> ids) {
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 根据id查询套餐菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id") String  id){
        SetmealDto setmealDto = setmealService.selectById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @param request
     * @return
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto,HttpServletRequest request){
        String username = tokenToUsername(request);
        setmealDto.setUpdateUser(username);
        setmealService.update(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 批量上架和下架
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(@PathVariable("status") Integer status, @RequestParam("ids") List<String> ids) {
        setmealService.status(status,ids);
        return R.success("修改成功");
    }


    /**
     * 利用token获得当前页面用户名
     *
     * @param request
     * @return
     */
    public String tokenToUsername(HttpServletRequest request) {
        //用户id设置
        String token = request.getHeader("Authorization");

        String username = (String) redisTemplate.opsForValue().get(token);

        return username;
    }

}
